package com.letter.otools.day;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.letter.otools.day.diary.DiaryFragment;
import com.letter.otools.day.todo.TodoFragment;
import com.letter.otools.day.user.AboutUsActivity;
import com.letter.otools.day.user.SettingActivity;
import com.letter.otools.day.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private BottomNavigationView navView;
    private NoScrollViewPager viewPager;
    ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.activity_main_home;
    }

    @Override
    protected void initView() {
        fragments.clear();
        viewPager.removeAllViews();
        fragments.add(new DiaryFragment());
//        fragments.add(new TodoFragment());
        bindFragement(fragments);
        navView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                viewPager.setCurrentItem(0, false);
            } else if (itemId == R.id.navigation_todo) {
                viewPager.setCurrentItem(1, false);
            }
            return true;
        });
    }

    protected void bindFragement(List<Fragment> fragments) {
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        viewPager.setAdapter(new MainAdapter(getSupportFragmentManager(), fragments));
    }

    private class MainAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {

        private List<Fragment> fragments = new ArrayList<>();

        public MainAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments.addAll(fragments);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (0 == position) {
            } else if (2 == position) {
            } else if (3 == position) {
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public static boolean isAlive = false;

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public void initBinder() {
        navView = findViewById(R.id.nav_view);
        viewPager = findViewById(R.id.appContainer);
    }
}
